﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace inf_1007_devoir3
{
    public partial class frmListeLocations : Form
    {
        private int ordreCourant;
        private List<Location> locations;

        public frmListeLocations()
        {
            ordreCourant = 2;
            InitializeComponent();
        }

        private void frmListeLocations_Load(object sender, EventArgs e)
        {
            remplirListe();
        }

        public void remplirListe()
        {
            string[] recherche = new string[5];

            recherche[0] = txtClient.Text.Trim();
            recherche[1] = txtVehicule.Text.Trim();
            recherche[2] = txtDateDebut.Text.Trim();
            recherche[3] = txtDateFin.Text.Trim();
            recherche[4] = txtDateRetour.Text.Trim();

            locations = CtrlLocation.getInstance().getLocations(ordreCourant, recherche);

            remplirLstLocations();
        }

        private void remplirLstLocations()
        {
            lstLocations.Items.Clear();

            foreach (Location loc in locations)
            {
                lstLocations.Items.Add(new ListViewItem(loc.getLigneListe()));
            }
        }

        private void btnRecherche_Click(object sender, EventArgs e)
        {
            remplirListe();
        }

        private void lstLocations_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            ordreCourant = e.Column;
            remplirListe();
        }

        private void btnAjouter_Click(object sender, EventArgs e)
        {
            CtrlLocation.getInstance().afficherNouvelleLocation(this);
        }

        private void btnModifier_Click(object sender, EventArgs e)
        {
            if (lstLocations.SelectedItems.Count == 1)
            {
                CtrlLocation.getInstance().afficherLocation(this, locations[lstLocations.SelectedIndices[0]]);
            }
        }

        private void lstLocations_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstLocations.SelectedItems.Count == 1)
            {
                btnModifier.Enabled = true;
                btnRetourner.Enabled = true;
            }
            else
            {
                btnModifier.Enabled = false;
                btnRetourner.Enabled = false;
            }

            if (lstLocations.SelectedItems.Count > 0)
            {
                btnSupprimer.Enabled = true;
            }
            else
            {
                btnSupprimer.Enabled = false;
            }
        }

        private void btnSupprimer_Click(object sender, EventArgs e)
        {
            if (lstLocations.SelectedItems.Count > 0)
            {
                if (MessageBox.Show("Voulez-vous vraiment supprimer les éléments sélectionnés ?", "Suppression", MessageBoxButtons.YesNo) == DialogResult.Yes)
                { 
                    foreach (int indice in lstLocations.SelectedIndices)
                    {
                        locations[indice].supprimer();
                    }

                    remplirListe();
                }
                
            }
        }

        private void btnRetourner_Click(object sender, EventArgs e)
        {
            if (lstLocations.SelectedItems.Count == 1)
            {
                CtrlLocation.getInstance().afficherRetour(this, locations[lstLocations.SelectedIndices[0]]);
            }
        }
    }
}
